import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ResourceBundle;

import javax.swing.BorderFactory;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

class DiagramPanel extends JPanel implements ActionListener, ItemListener  {

  private static final long serialVersionUID = -1789551003424858306L;
  
  private int squareX = 50;
    private int squareY = 50;
    private int squareW = 20;
    private int squareH = 20;
    //...where instance variables are declared:
    JPopupMenu fPopup;
    ResourceBundle fBundle = null;
    
    public DiagramPanel(ResourceBundle bundle) {

      fBundle = bundle;
      
      setBorder(BorderFactory.createLineBorder(Color.black));
      
      //...where the GUI is constructed:
      //Create the popup menu.
      fPopup = new JPopupMenu();
      JMenuItem menuItem = new JMenuItem(fBundle.getString("CreateNewElement"));
      menuItem.addActionListener(this);
      fPopup.add(menuItem);
      menuItem = new JMenuItem("Another popup menu item");
      menuItem.addActionListener(this);
      fPopup.add(menuItem);

      //Add listener to components that can bring up popup menus.
      PopupListener popupListener = new PopupListener();
      popupListener.setPopup(fPopup);
      addMouseListener(popupListener);

      /*addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
              moveSquare(e.getX(),e.getY());
          }
      });*/
      
    }
    
    private void moveSquare(int x, int y) {
      int OFFSET = 1;
      if ((squareX!=x) || (squareY!=y)) {
        repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
        squareX=x;
        squareY=y;
        repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
      } 
    }
    

    public Dimension getPreferredSize() {
      return new Dimension(250,200);
    }
    
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);       
      g.drawString("This is my custom Panel!",10,20);
      g.setColor(Color.RED);
      g.fillRect(squareX,squareY,squareW,squareH);
      g.setColor(Color.BLACK);
      g.drawRect(squareX,squareY,squareW,squareH);
    }

  @Override
  public void itemStateChanged(ItemEvent arg0) {
  	// TODO Auto-generated method stub
  	
  }

  @Override
  public void actionPerformed(ActionEvent arg0) {
  	// TODO Auto-generated method stub
  	arg0.getActionCommand();
  }  
}